/****************************************************************************
    
    analogDelay_gtk.cpp - simple AnalogDelay
    
    Copyright (C) 2007  Robert Gruendler <r.gruendler@gmail.com>
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307  USA

****************************************************************************/


#include "lv2plugin.hpp"
#include "analogdelay.peg"
#include "lv2-midifunctions.h"
#include "lv2advanced.hpp"
#include <iostream>


class AnalogDelay : public LV2::Advanced {
public:

	AnalogDelay(double rate, const char* bundle_path, const LV2_Host_Feature* const* f)
	: LV2::Advanced(k_n_ports)

	{
		gain = 0;


	}

	~AnalogDelay() {



	}


	void run(uint32_t nframes) {

		const float* const input_L = p(2);
		const float* const input_R = p(3);
		float* const output_L = p(4);
		float* const output_R = p(5);

		gain = *p(6);

		LV2_MIDIState in = { static_cast<LV2_MIDI*>(p<LV2_MIDI>(k_midi_input)), nframes, 0 };
		LV2_MIDIState out = { static_cast<LV2_MIDI*>(p<LV2_MIDI>(k_midi_output)), nframes, 0 };

		out.midi->size = 0;
		out.midi->event_count = 0;


		for (int i = 0; i < nframes; i++) {

			double timestamp;
			uint32_t event_size;
			unsigned char* data;

			lv2midi_get_event(&in, &timestamp, &event_size, &data);
			lv2midi_step(&in);

			if (timestamp < nframes && event_size == 3) {

				unsigned char type = data[0] & 0xF0;

				// Note on
				if (type == 0x90) {

					int note = data[1];
					int velocity = data[2];
					std::cout << "Note on -> pitch: " << note << " velocity: " << velocity << std::endl;

				}

				// Note off

				else if (type == 0x80) {

					int note = data[1];
					int velocity = data[2];
					std::cout << "Note off -> pitch: " << note << " velocity: " << velocity << std::endl;

				}

			}


			output_L[i] = input_L[i] * gain;
			output_R[i] = input_R[i] * gain;

		}
	}

public:

	char* command(uint32_t argc, const char*const* argv) {

		/*
		if (argc == 2 && !strcmp(argv[0], "foo")) {


		}
		 */
	}

private:

	float gain;

};


static LV2::RegisterAdvanced<AnalogDelay> reg(k_uri);
